package com.assets.asset.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.assets.asset.annotation.ReceiveComDeptID;
import com.assets.asset.service.ITAssetService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.assets.common.annotation.Log;
import com.assets.common.core.controller.BaseController;
import com.assets.common.core.domain.AjaxResult;
import com.assets.common.enums.BusinessType;
import com.assets.asset.domain.TReceive;
import com.assets.asset.service.ITReceiveService;
import com.assets.common.utils.poi.ExcelUtil;
import com.assets.common.core.page.TableDataInfo;

/**
 * 领用Controller
 *
 * @author bin
 * @date 2023-03-13
 */
@RestController
@RequestMapping("/asset/receive")
public class TReceiveController extends BaseController
{
    @Autowired
    private ITReceiveService tReceiveService;

    @Autowired
    private ITAssetService tAssetService;
    /**
     * 查询领用列表
     */
    @PreAuthorize("@ss.hasPermi('asset:receive:list')")
    @GetMapping("/list")
    public TableDataInfo list(TReceive tReceive)
    {
        startPage();
        List<TReceive> list = tReceiveService.selectTReceiveList(tReceive);
        return getDataTable(list);
    }

    /**
     * 导出领用列表
     */
    @PreAuthorize("@ss.hasPermi('asset:receive:export')")
    @Log(title = "领用", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TReceive tReceive)
    {
        List<TReceive> list = tReceiveService.selectTReceiveList(tReceive);
        ExcelUtil<TReceive> util = new ExcelUtil<TReceive>(TReceive.class);
        util.exportExcel(response, list, "领用数据");
    }

    /**
     * 获取领用详细信息
     */
    @PreAuthorize("@ss.hasPermi('asset:receive:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        AjaxResult ajax = AjaxResult.success(tReceiveService.selectTReceiveById(id));
        ajax.put("assetUsers",tAssetService.selectAllUserList());
        return ajax;
    }

    /**
     * 新增领用
     */
    @PreAuthorize("@ss.hasPermi('asset:receive:add')")
    @Log(title = "领用", businessType = BusinessType.INSERT)
    @PostMapping
    @ReceiveComDeptID
    public AjaxResult add(@RequestBody TReceive tReceive)
    {

        return success(tReceiveService.insertTReceive(tReceive));
    }

    /**
     * 修改领用
     */
    @PreAuthorize("@ss.hasPermi('asset:receive:edit')")
    @Log(title = "领用", businessType = BusinessType.UPDATE)
    @PutMapping
    @ReceiveComDeptID
    public AjaxResult edit(@RequestBody TReceive tReceive)
    {
        return toAjax(tReceiveService.updateTReceive(tReceive));
    }

    /**
     * 删除领用
     */
    @PreAuthorize("@ss.hasPermi('asset:receive:remove')")
    @Log(title = "领用", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tReceiveService.deleteTReceiveByIds(ids));
    }

    /**
     * 获取领用人修改资产领用人  改用AOP
     */
//    @PreAuthorize("@ss.hasPermi('asset:receive:query')")
//    @GetMapping(value = "/reject/{recipient}")
//    public AjaxResult getRecipient(@PathVariable("assetCode") String assetCode,@PathVariable("recipient") String recipient)
//    {
//        //获取领用人
//        return success(tReceiveService.updateAssetRecipient(assetCode,recipient));
//    }





}
